package com.han.controllers.export;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jmesa.core.filter.MatcherKey;
import org.jmesa.core.filter.NumberFilterMatcher;
import org.jmesa.limit.ExportType;
import org.jmesa.model.TableModel;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
import org.jmesa.view.editor.CellEditor;
import org.jmesa.view.html.HtmlBuilder;
import org.jmesa.view.html.component.HtmlColumn;
import org.jmesa.view.html.component.HtmlRow;
import org.jmesa.view.html.component.HtmlTable;

import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.Student;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.ExamResponseDAO;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class JMesaExportStudentExamListServlet
 */
@WebServlet("/Professor/JMesaExportStudentExamListServlet")
public class JMesaExportStudentExamListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JMesaExportStudentExamListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = null;
		ExamResponseDAO examresponsedao = new ExamResponseDAO();
		ExamDAO examdao = new ExamDAO();
		UserDAO userdao = new UserDAO();
		
		if(request.getParameter("id")!=null){
			id = request.getParameter("id");
		}else if(request.getSession().getAttribute("examID")!=null){
			id = (String) request.getSession().getAttribute("examID");
		}
		else{
			//error
		}
		
		ArrayList<Student> students = null;
		Exam exam = null;
		ArrayList<ExamResponse> examresponses = new ArrayList<ExamResponse>();
		
		try {
			students = userdao.retrieveStudentsByExamTaken(id);
			
			for(Student student:userdao.retrieveStudentsByExamMissed(id)){
				if(!students.contains(student)){
					students.add(student);
				}
			}
			
			exam = examdao.retrieveExamById(id);
			//System.out.println(students);
			for(Student student:students){
			
				ArrayList<ExamResponse> tempERList = examresponsedao.retrieveResponsesByStudentAndExam(student.getUserID(), exam);
				
				examresponses.addAll(tempERList);
				
//				for(ExamResponse er: tempERList){
//					er = examresponsedao.retrieveResponseById(er.getExamResponseID());
//				}
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		TableModel tableModel = new TableModel("table", request, response);
		
		tableModel.setExportTypes(ExportType.CSV,ExportType.JEXCEL,ExportType.PDFP);
		tableModel.setItems(examresponses);
		
		if(tableModel.isExporting()){
			Table Table = new Table().caption("Examinee-Exam Record");
			
			Row Row = new Row();
			Table.setRow(Row);
			
			Column sid = new Column("studentID").title("Examinee ID");
			Row.addColumn(sid);
		
			Column term = new Column("dateTaken").title("Date Taken");
			//tableModel.addFilterMatcher(new MatcherKey(Double.class, "studentGrade"), new NumberFilterMatcher("#.##"));
			Row.addColumn(term);
	
			Column career = new Column("grade").title("Grade");
			Row.addColumn(career); 
	
			tableModel.setTable(Table);
			
			String export = tableModel.render();
			
			request.setAttribute("table",export);
		}
		else{
			HtmlTable htmlTable = new HtmlTable();
	
			HtmlRow htmlRow = new HtmlRow();
			htmlTable.setRow(htmlRow);
						
			HtmlColumn studentIDCol = new HtmlColumn("studentID").title("Examinee ID");
	    	htmlRow.addColumn(studentIDCol); 
	
	    	HtmlColumn dateCol = new HtmlColumn("dateTaken").title("Date Taken");
	    	htmlRow.addColumn(dateCol);
	
	    	HtmlColumn gradeCol = new HtmlColumn("grade").title("Grade");
	    	tableModel.addFilterMatcher(new MatcherKey(Double.class, "grade"), new NumberFilterMatcher("###.##"));
	    	htmlRow.addColumn(gradeCol);
	    	
	    	HtmlColumn viewLink = new HtmlColumn("name").title("View");
	    	viewLink.setCellEditor(new CellEditor() {             
				public Object getValue(Object item, String property, int rowcount) {                 
	                     
	    			HtmlBuilder html = new HtmlBuilder();
	    			ExamResponse er = (ExamResponse)item;
	    			html.a().href().quote().append("JMesaExportStudentExamServlet?id="+er.getExamResponseID()).quote().close();
	    			html.append("View Response");
	    			html.aEnd();
	    			return html.toString();             
	    			}
				
	    		});
	    	viewLink.setSortable(false);
	    	viewLink.setFilterable(false);
	    	htmlRow.addColumn(viewLink); 
	
			tableModel.setTable(htmlTable);
	
			String html = tableModel.render();
			
			request.setAttribute("table",html);
			RequestDispatcher rd = request.getRequestDispatcher("Report_StudentExams.jsp");
			rd.forward(request, response);	
		}
	}

}
